package com.sias.realestate.web.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.sias.realestate.web.model.Charge;
import com.sias.realestate.web.model.Notification;
import com.sias.realestate.web.model.Owner;
import com.sias.realestate.web.model.User;
import com.sias.realestate.web.service.ChargeService;
import com.sias.realestate.web.service.NotificationService;
import com.sias.realestate.web.service.OwnerService;
import com.sias.realestate.web.service.UserService;
import com.sias.realestate.web.util.FinalVarUtil;
import com.sias.realstate.web.pagemodel.PageModel;

@Controller
@RequestMapping(value = "/charge")
public class ChargeController {

	@Resource
	private ChargeService chargeService;
	@Resource
	private NotificationService notificationService;
	@Resource
	private UserService userService;
	@Resource
	private OwnerService ownerService;

	@RequestMapping(value = "/getAllCharge", method = RequestMethod.GET)
	public ModelAndView getAllCharge(HttpServletRequest request){
		ModelAndView mv = new ModelAndView();
		PageModel pageModel = new PageModel();
		List<Charge> list = chargeService.selectList();
		String th_list = FinalVarUtil.chargeTh;
		pageModel.setTh_list(th_list);
		mv.addObject("pageModel", pageModel);
		mv.addObject("list", list);
		mv.setViewName("charge/chargeList");
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：进入收缴费用添加界面 创建日期：2016年12月26日下午2:33:12
	 */
	@RequestMapping(value = "/addChargeJsp", method = RequestMethod.GET)
	public ModelAndView addChargeJsp(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("charge/addCharge");
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：添加收缴费用 创建日期：2016年12月26日下午2:33:12
	 */
	@RequestMapping(value = "/addCharge", method = RequestMethod.POST)
	public ModelAndView addCharge(HttpServletRequest request, Charge charge) {
		ModelAndView mv = new ModelAndView();
		List<Owner> owner = ownerService.selectCheckin();
		if (owner != null) {
			for (int i = 0; i < owner.size(); i++) {
				charge.setoId(owner.get(i).getoId());
				charge.setcStatus("0");
				chargeService.insert(charge);
			}
			request.setAttribute("successInfo", "添加收缴费用信息成功！");
			request.setAttribute("page_code", "allcharge");
			mv.setViewName("/success-info");
		} else {
			request.setAttribute("failedInfo", "添加收缴费用信息失败，小区没有住户！");
			request.setAttribute("page_code", "allcharge");
			mv.setViewName("/failed-info");
		}
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：发送催缴费通知 创建日期：2016年12月14日上午10:59:39
	 */
	@RequestMapping(value = "/sendNotification", method = RequestMethod.GET)
	public ModelAndView sendNotification(HttpServletRequest request, Integer oid, Integer cid) {
		String type = null;
		Notification notification = new Notification();
		Charge charge = chargeService.findChargeByCid(cid);
		Owner owner = ownerService.selectById(oid);
		if (charge.getprice().getpType().equals("1")) {
			type = "水费";
		}
		if (charge.getprice().getpType().equals("2")) {
			type = "电费";
		}
		if (charge.getprice().getpType().equals("3")) {
			type = "燃气费";
		}
		if (charge.getprice().getpType().equals("4")) {
			type = "宽带费";
		}
		if (charge.getprice().getpType().equals("5")) {
			type = "物业管理费";
		}
		notification.setoId(oid);
		notification.setnType(0);
		notification.setnTime(new Date());
		notification.setnContent("亲爱的业主 " + owner.getoName() + " 您好，您的" + type + "费用该交了！");
		notificationService.insert(notification);// 添加一条通知消息

		ModelAndView mv = new ModelAndView();
		PageModel pageModel = new PageModel();
		List<Charge> list = chargeService.selectList();
		String th_list = FinalVarUtil.chargeTh;
		pageModel.setTh_list(th_list);
		mv.addObject("pageModel", pageModel);
		mv.addObject("list", list);
		mv.setViewName("charge/chargeList");
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：我的缴费记录信息 创建日期：2016年12月13日下午4:22:12
	 */
	@RequestMapping(value = "/getMyCharge", method = RequestMethod.GET)
	public ModelAndView getMyCharge(HttpServletRequest request, User user) {
		ModelAndView mv = new ModelAndView();
		PageModel pageModel = new PageModel();
		user = (User) request.getSession().getAttribute("user");
		Owner owner = ownerService.findByIdcard(user.getuIdcard());
		List<Charge> list = null;
		if (owner != null) {
			list = chargeService.findChargeByOid(owner.getoId());// 根据oid查询业主的缴费记录
		}
		String th_list = FinalVarUtil.mychargeTh; // 获取我的缴费记录表头
		pageModel.setTh_list(th_list);
		mv.addObject("pageModel", pageModel);
		mv.addObject("list", list);
		mv.setViewName("charge/mychargeList");
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：审核通过该消息 创建日期：2016年12月23日上午10:24:13
	 */
	@RequestMapping(value = "/changeChargeStatus", method = RequestMethod.GET)
	public ModelAndView changeChargeStatus(HttpServletRequest request, Integer cid) {
		ModelAndView mv = new ModelAndView();
		PageModel pageModel = new PageModel();
		Charge charge = chargeService.findChargeByCid(cid);
		charge.setcStatus("1");
		chargeService.update(charge);
		List<Charge> list = chargeService.selectList();
		String th_list = FinalVarUtil.chargeTh;
		pageModel.setTh_list(th_list);
		mv.addObject("pageModel", pageModel);
		mv.addObject("list", list);
		mv.setViewName("charge/chargeList");
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：确认缴费 创建日期：2016年12月23日上午10:24:37
	 */
	@RequestMapping(value = "/conmitCharge", method = RequestMethod.GET)
	public ModelAndView conmitCharge(HttpServletRequest request, User user, Integer cid) {
		ModelAndView mv = new ModelAndView();
		PageModel pageModel = new PageModel();
		user = (User) request.getSession().getAttribute("user");
		Owner owner = ownerService.findByIdcard(user.getuIdcard());
		Charge charge = chargeService.findChargeByCid(cid);
		charge.setcTime(new Date());
		charge.setcStatus("2");
		chargeService.update(charge);
		List<Charge> list = chargeService.findChargeByOid(owner.getoId());// 根据oid查询业主的缴费记录
		String th_list = FinalVarUtil.mychargeTh; // 获取我的缴费记录表头
		pageModel.setTh_list(th_list);
		mv.addObject("pageModel", pageModel);
		mv.addObject("list", list);
		mv.setViewName("charge/mychargeList");
		return mv;
	}

}
